
var parkingService = require('../services/miniAppService');
var messageUtil = require('../common/utils/messageUtil');
//var toolUtil = require('../common/utils/toolUtil');

Date.prototype.addHours = function(h) {
    this.setTime(this.getTime() + (h*60*60*1000));
    return this;
};

var hourDiff = 8;

function addHours(data, hours){
    var hrs = hours ? hours : hourDiff;
    for(var k in data){           // adding hours
        if(data[k] instanceof Date){
            data[k].addHours(hrs);
        }
    }
}

function serviceFunc(req,res,fnName,err,result,code){
    code = code == null ? 1000: code;
    if(result.list instanceof Array){
        result.list.forEach(function(r){
            addHours(r,hourDiff);
        })
    }else{
        addHours(result,hourDiff);
    }
    if (err) {
        logger.error('Failed to ' + fnName + '...!' + err.message);
        res.json(messageUtil.composeFailedMessage(req, err.message, code));
    } else {
        res.json(messageUtil.composeSucceededMessage(req, null, result, code));
    }
}

exports.getParkingList = function (req, res) {
    var ts = req.params.ts;
    var direction = req.query.direction;

    parkingService.getParkingListService(ts, direction, function (err, result) {
        serviceFunc(req,res,'getParkingListService',err,result);
    })
};

exports.getParkingDetail = function (req, res) {
    var wishId = req.params.wishId;
    parkingService.getParkingDetailService(wishId, function (err, result) {
        serviceFunc(req,res,'getParkingDetailService',err,result);
    })
};

exports.makeParkingItem = function (req, res) {
    var user = req.params.wechatId;
    var input = req.body;

    console.log(input);

    // res.json(messageUtil.composeSucceededMessage(req, null, null, 1000));

    // return;

    parkingService.makeParkingService(user, input, function (err, result) {
        serviceFunc(req,res,'makeParkingService',err,result);
    })
};

